﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using ComLib.Configuration;
using ComLib.Collections;


namespace ComLib.Web.Modules.Services
{
    /// <summary>
    /// Represents a specific entry in the Dashboard model section.
    /// </summary>
    public class ModelEntry
    {
        public Type Model;
        public string Alias;
        public string IndexUrlPart;
        public string ManageUrlPart;
        public bool IsOnlyForAdmin;
        /*
        public string RolesForCreate;
        public string RolesForUpdate;
        public string RolesForRetrieve;
        public string RolesForDelete;
        public string RolesForManage;
        public string RolesForIndex;
        */

        /// <summary>
        /// Intialize using the model type and use defaults.
        /// </summary>
        /// <param name="model"></param>
        public ModelEntry(Type model) : this(model, null, null, null)
        {
        }


        /// <summary>
        /// Initialize using the various properties.
        /// </summary>
        /// <param name="model"></param>
        /// <param name="alias"></param>
        /// <param name="indexUrlPart"></param>
        /// <param name="manageUrlPart"></param>
        public ModelEntry(Type model, string alias, string indexUrlPart, string manageUrlPart)
        {
            Model = model;
            Alias = string.IsNullOrEmpty(alias) ? model.Name : alias;
            IndexUrlPart = string.IsNullOrEmpty(indexUrlPart) ? "index" : indexUrlPart;
            ManageUrlPart = string.IsNullOrEmpty(manageUrlPart) ? "manage" : manageUrlPart;
        }


        /// <summary>
        /// Get the name to display.
        /// </summary>
        public string Name
        {
            get { return string.IsNullOrEmpty(Alias) ? Model.Name : Alias; }
        }


        /*
        public void DefaultAllBehaviourToAdmin()
        {
            IsOnlyForAdmin = true;
        }


        /// <summary>
        /// Defaults all the crud roles to admin role.
        /// </summary>
        /// <returns></returns>
        public void DefaultAllCrudRolesToAdmin()
        {
            RolesForCreate = "Admin";
            RolesForUpdate = "Admin";
            RolesForRetrieve = "Admin";
            RolesForDelete = "Admin";
            RolesForManage = "Admin";
            RolesForIndex = "Admin";
        }


        /// <summary>
        /// Set roles
        /// </summary>
        /// <param name="createRoles"></param>
        public void SetRoles(string createRoles, string updateRoles, string retrieveRoles, string deleteRoles, string delete)
        {
        }
        */
    }



    /// <summary>
    /// Class to handle models
    /// </summary>
    public class DashboardModels : GenericListBase<ModelEntry>
    {       
        /// <summary>
        /// Add a list of models that should be shown in the dashboard on the sidebar.
        /// </summary>
        /// <param name="models"></param>
        public void Add(params Type[] models)
        {
            if (models == null || models.Length == 0)
                return;

            // Add the default definitions
            foreach (Type model in models)
                this.Add(new ModelEntry(model));
        }
    }



    /// <summary>
    /// Class to represent the dashboard.
    /// </summary>
    public class Dashboard
    {
        private static DashboardModels _models = new DashboardModels();
        private static IDictionary<string, IConfigSourceBase> _configs = new Dictionary<string, IConfigSourceBase>();


        /// <summary>
        /// Get the models.
        /// </summary>
        public static DashboardModels Models
        {
            get { return _models; }
        }



        /// <summary>
        /// Get the configs.
        /// </summary>
        public static IDictionary<string, IConfigSourceBase> Configs
        {
            get { return _configs; }
        }
    }
}
